﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;


namespace Nivdal
{
    public partial class NewsTranslationPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            if (ou.TranslatesTo == "0")
                Response.Redirect("~/Default.aspx");

            Title = "Nivdal.com! News item translation page";
            if (!IsPostBack)
            {
                TranslateDataSource.SelectParameters.Add("Language", ou.TranslatesTo);
            }

        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected void Translate(object sender, EventArgs e)
        { 
            OnlineUser ou = (OnlineUser)Session["User"];
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData(); 
            string NewsTitle = (NewsItemTranslationFormView.FindControl("TranslatedTitle") as TextBox).Text.Trim();
            string Body = GlobalFunctions.EncodeHtmlFromText((NewsItemTranslationFormView.FindControl("NewsTextArea") as TextBox).Text.Trim());
            string SourceItem = (NewsItemTranslationFormView.FindControl("SourceItemIdHiddenField") as HiddenField).Value;
            string Answer = GND.ExecNivdalSQL("EnglishItemId", "Title", "Text", "Language", "UserId",
                "Nivdal_TranslateNewsItem", SourceItem, NewsTitle, Body, ou.TranslatesTo, ou.UserId);
            MessageLabel.Text = Answer;
            NewsItemTranslationFormView.DataBind();
        }

    }
}